package com.have.blog.web.controller;

import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.models.OuterLink;
import com.have.blog.model.models.Tag;
import com.have.blog.web.constant.BlogConstant;
import com.have.blog.web.dto.BlogDto;
import com.have.blog.web.dto.WebConfigDto;
import com.have.blog.web.dto.WebNavigationBarDto;
import com.have.blog.web.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/index")
@Api(value = "首页相关接口", tags = {"首页相关接口"})
@Slf4j
public class IndexController {


    @Autowired
    private WebBlogService blogService;
    @Autowired
    private WebConfigService webConfigService;
    @Autowired
    private WebNavbarService webNavbarService;
    @Autowired
    private SystemParamService systemParamService;
    @Autowired
    private LinkService linkService;
    @Autowired
    private TagService tagService;


    @ApiOperation(value = "通过推荐等级获取博客列表", notes = "通过推荐等级获取博客列表")
    @GetMapping("/getBlogByLevel")
    public Wrapper<CommonPage> getBlogByLevel(@ApiParam(name = "level", value = "推荐等级", required = false) @RequestParam(name = "level", required = false, defaultValue = "0") Integer level,
                                              @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                              @ApiParam(name = "useSort", value = "使用排序", required = false) @RequestParam(name = "useSort", required = false, defaultValue = "0") Integer useSort) {
        List<BlogDto> blogList = blogService.getBlogPageByLevel(level, currentPage, useSort);
        log.info("通过推荐等级{}，获取博客:{}", level, blogList.size());
        return Wrapper.success(CommonPage.restPage(blogList));
    }

    @ApiOperation(value = "获取首页排行博客", notes = "获取首页排行博客")
    @GetMapping("/getHotBlog")
    public Wrapper<CommonPage> getHotBlog() {
        log.info("获取首页排行博客");
        List<BlogDto> blogList = blogService.getHotBlog();
        return Wrapper.success(CommonPage.restPage(blogList));
    }

    @ApiOperation(value = "获取首页最新的博客", notes = "获取首页最新的博客")
    @GetMapping("/getNewBlog")
    public Wrapper<CommonPage> getNewBlog(HttpServletRequest request,
                             @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                             @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        log.info("获取首页最新的博客");
        List<BlogDto> blogList = blogService.getNewBlog(currentPage, pageSize);
        return Wrapper.success(CommonPage.restPage(blogList));
    }

    @ApiOperation(value = "mogu-search调用获取博客的接口[包含内容]", notes = "mogu-search调用获取博客的接口")
    @GetMapping("/getBlogBySearch")
    public Wrapper<CommonPage> getBlogBySearch(HttpServletRequest request,
                                  @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                  @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        log.info("搜索博客");
        List<BlogDto> blogList= blogService.getBlogBySearch(currentPage, pageSize);
        return Wrapper.success(CommonPage.restPage(blogList));
    }


    @ApiOperation(value = "获取网站配置", notes = "获取友情链接")
    @GetMapping("/getWebConfig")
    public Wrapper getWebConfig() {
        log.info("获取网站配置");
        WebConfigDto webConfig = webConfigService.getWebConfig();
        return Wrapper.success(webConfig);
    }


    @ApiOperation(value = "获取网站导航栏", notes = "获取网站导航栏")
    @GetMapping("/getWebNavbar")
    public Wrapper getWebNavbar() {
        log.info("获取网站导航栏");
        List<WebNavigationBarDto> allList = webNavbarService.getAllList();
        return Wrapper.success(allList);
    }

//    @ApiOperation(value = "按时间戳获取博客", notes = "按时间戳获取博客")
//    @GetMapping("/getBlogByTime")
//    public String getBlogByTime(HttpServletRequest request,
//                                @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
//                                @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {
//
//        String blogNewCount = sysParamsService.getSysParamsValueByKey(SysConf.BLOG_NEW_COUNT);
//        return Wrapper.result(SysConf.SUCCESS, blogService.getBlogByTime(currentPage, Long.valueOf(blogNewCount)));
//    }
//
    @ApiOperation(value = "获取最热标签", notes = "获取最热标签")
    @GetMapping("/getHotTag")
    public Wrapper getHotTag() {
        String hotTagCount = systemParamService.getSystemParamValueByKey(BlogConstant.HOT_TAG_COUNT);
        // 从Redis中获取友情链接
        List<Tag> tagList = tagService.getHotTag(Integer.valueOf(hotTagCount));
        return Wrapper.success(tagList);
    }

    @ApiOperation(value = "获取友情链接", notes = "获取友情链接")
    @GetMapping("/getLink")
    public Wrapper getLink() {
        String friendlyLinkCount = systemParamService.getSystemParamValueByKey(BlogConstant.FRIENDLY_LINK_COUNT);
        List<OuterLink> linkList = linkService.getListByPageSize(Integer.valueOf(friendlyLinkCount));
        return Wrapper.success(linkList);
    }
//
////    @BussinessLog(value = "点击友情链接", behavior = EBehavior.FRIENDSHIP_LINK)
//    @ApiOperation(value = "增加友情链接点击数", notes = "增加友情链接点击数")
//    @GetMapping("/addLinkCount")
//    public String addLinkCount(@ApiParam(name = "uid", value = "友情链接UID", required = false) @RequestParam(name = "uid", required = false) String uid) {
//        log.info("点击友链");
//        return linkService.addLinkCount(uid);
//    }
//

//    @BussinessLog(value = "记录访问页面", behavior = EBehavior.VISIT_PAGE)
    @ApiOperation(value = "记录访问页面", notes = "记录访问页面")
    @GetMapping("/recorderVisitPage")
    public Wrapper recorderVisitPage(@ApiParam(name = "pageName", value = "页面名称", required = false) @RequestParam(name = "pageName", required = true) String pageName) {

        return Wrapper.success();
    }

}
